package panels;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class DiceRoller extends JPanel implements PropertyChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6943372352110894968L;
	public static final String CHANGED_PROPERTY = "changed";
	
	private JButton clearButton;
	private DiceButtonPanel diceButtonPanel;
	private DiceButtonPanel diceButtonPanel_1;
	private DiceButtonPanel diceButtonPanel_2;
	private DiceButtonPanel diceButtonPanel_3;
	private DiceButtonPanel diceButtonPanel_4;
	private DiceButtonPanel diceButtonPanel_5;
	private DiceButtonPanel diceButtonPanel_6;
	private DiceButtonPanel diceButtonPanel_7;

	public static final String CHANGED = "changed";
	
	/**
	 * Create the panel.
	 */
	public DiceRoller() {
		
		
		
		setBorder(new TitledBorder(null, "Dice Roller", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new GridLayout(1, 2, 0, 0));
		
		JPanel newPanel = new JPanel();
		newPanel.setBorder(null);
		add(newPanel);
		newPanel.setLayout(new GridLayout(9, 1, 0, 0));


		diceButtonPanel = new DiceButtonPanel(2);
		diceButtonPanel.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel);
		
		diceButtonPanel_1 = new DiceButtonPanel(4);
		diceButtonPanel_1.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_1);
		
		diceButtonPanel_2 = new DiceButtonPanel(6);
		diceButtonPanel_2.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_2);
		
		diceButtonPanel_3 = new DiceButtonPanel(8);
		diceButtonPanel_3.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_3);
		
		diceButtonPanel_4 = new DiceButtonPanel(10);
		diceButtonPanel_4.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_4);
		
		diceButtonPanel_5 = new DiceButtonPanel(12);
		diceButtonPanel_5.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_5);
		
		diceButtonPanel_6 = new DiceButtonPanel(20);
		diceButtonPanel_6.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_6);
		
		diceButtonPanel_7 = new DiceButtonPanel(100);
		diceButtonPanel_7.addPropertyChangeListener(DiceButtonPanel.CHANGED_PROPERTY, this);
		newPanel.add(diceButtonPanel_7);
		
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				diceButtonPanel.clear();
				diceButtonPanel_1.clear();
				diceButtonPanel_2.clear();
				diceButtonPanel_3.clear();
				diceButtonPanel_4.clear();
				diceButtonPanel_5.clear();
				diceButtonPanel_6.clear();
				diceButtonPanel_7.clear();
				
				update();
			}
		});
		
		newPanel.add(clearButton);

		this.firePropertyChange(CHANGED, false, true);
	}
	
	public void update() {
		clearButton.setText("Sum = " + Integer.toString(getSum()));
		this.firePropertyChange(CHANGED, false, true);
//		invalidate();
	}
	
	public int getSum() {
		int sum = 0;
		sum += diceButtonPanel.getRollsSum();
		sum += diceButtonPanel_1.getRollsSum();
		sum += diceButtonPanel_2.getRollsSum();
		sum += diceButtonPanel_3.getRollsSum();
		sum += diceButtonPanel_4.getRollsSum();
		sum += diceButtonPanel_5.getRollsSum();
		sum += diceButtonPanel_6.getRollsSum();
		sum += diceButtonPanel_7.getRollsSum();
		return sum;
	}

	public JButton getClearButton() {
		return clearButton;
	}
	
	public void paint(Graphics G) {
		clearButton.setText("Sum = " + Integer.toString(getSum()));
		super.paint(G);		
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(DiceButtonPanel.CHANGED_PROPERTY)) {
			this.update();
		}
	}
	
	
	
	
}
